getStudentInfo();
function getStudentInfo() {
    Ajax({
        url: "http://localhost/project/server/queryStudentAll.php",
        success(res) {
            const tbody = document.querySelector("tbody")

            //将获取到的所有数据存储到sessionStorage里面去
            sessionStorage.setItem("allStudentInfo",res);

            res = JSON.parse(res)
            /* 一页数据显示10条 */
            let result = res.slice(0,10)
            result.forEach(item => {
                let str = `
                <tr>
                    <td>${item.s_id}</td>
                    <td>${item.s_name}</td>
                    <td>${item.s_username}</td>
                    <td>${item.s_age}</td>
                    <td>${item.c_name}</td>
                    <td>${item.s_gender}</td>
                    <td>
                    <button type="button" class="btn btn-warning btn-sm" data-o='update' data-id='${item.s_id}'>警告</button>
                    <button type="button" class="btn btn-danger btn-sm" data-o='del' data-id='${item.s_id}'>危险</button>
                    </td>
                </tr>
                `
                tbody.innerHTML += str;
            })
        }
    })
}
/* 实现删除和修改 */
deleteOrUpdate()
function deleteOrUpdate(){
    const tbody = document.querySelector("tbody")
    tbody.addEventListener("click",function(e){
        e = e || window.event;
        console.log(e.target.dataset.o);
        console.log(e.target.dataset.id);
        /* 删除操作 */
        if(e.target.dataset.o == 'del'){
            //防止误删
            const flag = window.confirm("您确定要进行删除吗?")
            if(flag){
                Ajax({
                    url:"http://localhost/project/server/studentDeleteById.php",
                    data:{id:e.target.dataset.id},
                    success(res){
                        location.reload();
                    }
                })
            }
        }
    })
}
/* 实现添加用户信息 */
function addStudentInfo(){
    let username = document.getElementById("addStudentUsername").value
    let name = document.getElementById("addStudentName").value
    let password = document.getElementById("addStudentPassword").value
    let age = document.getElementById("addStudentAge").value
    let classId = document.getElementById("addStudentClassId").value
    let gender = document.querySelectorAll(".addStudentGender")
    if(gender[0].checked){
        gender = '男'
    }else{
        gender = "女"
    }

    /* 非空判断 */
    if(!username || !name || !password || !age || !classId) return window.alert("请输入完整的数据")

    Ajax({
        url:"http://localhost/project/server/studentAdd.php",
        type:"POST",
        data:{username,password,name,gender,age,classId},
        success(res){
            console.log(res);
        }
    })
}